import Vue from 'vue'
import Vuex from 'vuex'
import orderPageStore from './modules/frontStore/orderPageStore';
import foodsSet from './modules/backStore/foodsSetPageStore';
import foodsTypeSet from './modules/backStore/foodsTypeSetStore'
import invoiceStore from './modules/frontStore/invoiceStore'
import addMemberStore from "./modules/frontStore/addMemberStore";
import workPageStore from "./modules/frontStore/workPageStore";
import indentStore from "./modules/frontStore/indentStore"
import selectMemberStore from "./modules/frontStore/selectMemberStore";
import dataManipulationStore from "./modules/backStore/dataManipulationStore";
import staffInformationStore from "./modules/backStore/staffInformationStore";
import wageManagementStore from "./modules/backStore/wageManagementStore";
import staffPositionStore from "./modules/backStore/staffPositionStore";
import staffPermissionsStore from "./modules/backStore/staffPermissionsStore";
import operationStore from "./modules/backStore/operationStore";
import restaurantTableStore from "./modules/backStore/restaurantTableStore";
import consumablesManagementStore from "./modules/backStore/consumablesManagementStore";
import consumablesTypeStore from "./modules/backStore/consumablesTypeStore"
import http from '../http/http'
import * as url from '../http/api'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //用户信息
    user:{
      userName:sessionStorage.getItem('userName')||undefined,
      userId:sessionStorage.getItem('userId')||undefined,
      loginTime:sessionStorage.getItem('loginTime')||undefined,
      token:sessionStorage.getItem('token')||undefined,
      power:sessionStorage.getItem('power')||undefined,
    },
    //用户操作信息
    userOperation:{
      userName:'',//操作名
      operationDate:'',//操作时间
      operationMessage:'',//操作内容
    },
    //获取当前时间
    nowTime:{
        time:'',
        date:'',
    },
    //表格样式
    headerCssObj:{    //改变表格表头的样式
      backgroundColor:'#aedaff',
      color:'#ffffff',
      textAlign:'center',
      fontSize:'16px'
    },
    cellCssObj:{     //改变表格样式
      textAlign:'center',
      fontSize:'16px',
    }
    //以上是对表格的统一样式，引用如下：
    //在el-table标签内加    :header-cell-style="this.$store.state.headerCssObj"    :cell-style="this.$store.state.cellCssObj"
  },
  mutations: {
    userLoginData(state,data){
      //console.log(data)
      state.user.userName = data.userName
      state.user.userId = data.userId
      state.user.loginTime = data.loginTime
      state.user.power = data.power
      state.user.token = data.token
    },
    removeUser(state){
      state.user.userName = undefined
      state.user.userId = undefined
      state.user.loginTime = undefined
      state.user.power = undefined
      state.user.token = undefined
    },
    userOperationData(state,data){
      state.userOperation.userName = data.userName
      state.userOperation.operationDate = data.operationDate
      state.userOperation.operationMessage = data.operationMessage
    },
    nowTime(state,data){
        state.nowTime.time = data.time
        state.nowTime.date = data.date
    }
  },
  actions: {
    userLogin({commit},obj){
      let userData = {
        userName:obj.name,
        userId:'',
        loginTime:'',
        token:'',
        power:'',
      }
      return http({
        url:url.userLogin,
        method:'post',
        data:obj,
      }).then(resp=>{
        if(resp.data.Code === 200){
          userData.token = resp.data.token
          userData.userId = resp.data.StaffId
          userData.loginTime = resp.data.LoginTime
          userData.power = resp.data.Power
          commit('userLoginData',userData)
          sessionStorage.setItem('userName',userData.userName)
          sessionStorage.setItem('userId',userData.userId)
          sessionStorage.setItem('loginTime',userData.loginTime)
          sessionStorage.setItem('power',userData.power)
          sessionStorage.setItem('token',userData.token)
        }
        return resp.data
      }).catch(err=>{
        return err
      })
    },
    outLogin({commit}){
      sessionStorage.removeItem('userName')
      sessionStorage.removeItem('userId')
      sessionStorage.removeItem('loginTime')
      sessionStorage.removeItem('token')
      sessionStorage.removeItem('power')
      commit('removeUser')
    },
    userOperation({commit},obj){
      let userObj = {
        logStaff:obj.userName,
        logTime:obj.operationDate,
        logType:obj.operationMessage,
      }
      console.log(userObj)
      return http({
        url:url.logManagementSave,
        method: 'post',
        data:userObj
      }).then(resp=>{
        console.log(resp,999)
        commit('userOperationData',obj)
      }).catch(err=>{
        console.log(err)
      })
    },
    getTime({commit}){
        let date = new Date();
        let yy = date.getFullYear();
        let mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
        let dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
        let hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
        let mf = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        let time = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
        commit('nowTime', {time, date})
    }
  },
  modules: {
    orderPageStore,
    foodsSet,
    foodsTypeSet,
    addMemberStore,
    invoiceStore,
    workPageStore,
    indentStore,
    selectMemberStore,
    dataManipulationStore,
    staffInformationStore,
    wageManagementStore,
    staffPositionStore,
    staffPermissionsStore,
    operationStore,
    restaurantTableStore,
    consumablesManagementStore,
    consumablesTypeStore,
  },
  getters:{
    getPower(state){
        return Number(state.user.power)
    },
    getName(state){
        return state.user.userName
    },
    getNowTime(state){
        return state.nowTime.time
    }
  }
})
// 查询按钮  <el-button type="primary" icon="el-icon-search">搜索</el-button>
//输入框  <el-input v-model="input" placeholder="请输入内容"></el-input>
//删除 <el-button size="mini">超小按钮</el-button> 颜色：409EFF
//增加 <el-button size="medium">中等按钮</el-button> 颜色 409EFF